package api_servers
{
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	public class api_okserver extends api_externalserver {
		private var _call_id: int = 0;
		private var _privacy: Object = {'PUBLIC':2, 'FRIENDS':4};
		
		override public function convertVars(): Object {
			return {
				'gender': {name: 'sex', cnv: function (value: String): Object {
						if (value == 'male') return 0
						else return 1;
					}
				},
				'name':{name:'nick'},
				'birthday':{name:'birthday', cnv: function (value: String): Object {
						var va: Array = value.split('-');
						va.reverse();
						return va.join('.');
					}},
				'pic_1': {name:'pic_small'},
				'pic_2': {name:'pic'},
				'pic_3': {name:'pic'},
				'pic_4': {name:'pic_big'},
				'type' : {name:'privacy', cnv: function (value: String): Object {
						return _privacy[value];
					}},
				'albums': {name: 'albums', cnv: function (value: Array): Object {
						argConvert(value);
						return value;
					}},
				'photos': {name: 'photos', cnv: function (value: Array): Object {
						argConvert(value);
						return value;
					}},
				'preview_url': {name:'src_small'},
				'standard_url': {name:'src'}
			}			
		}
		
		public function api_okserver(a_sroot:Object) {
			var a_vars: Object = Object(a_sroot.parent).loaderInfo.parameters;
			super(a_sroot, a_vars['api_server'] + 'api/', a_vars['logged_user_id'], a_vars['application_key'], 
					a_vars['session_secret_key'], 0);
			vars = a_vars;					
		}
		
		protected function okHelper(method: String, a_vars: Array, handler_: Function=null, a_cache: Boolean = false, request: URLRequest=null): void {
			jsrHelper(a_vars, handler_, a_cache?method:'', a_cache, api_url + method, request);
		} 
		
		override protected function srHelper( arr:Array): URLVariables {// sendRequestHelper
			
			var isUid: Boolean = arr.some(function(item:*, index:int, array:Array): Boolean {
				return item.pn == 'uid';
			});
			 
			arr.push({pn: "application_key", pv: app_id } );
			arr.push({pn: "call_id", pv: _call_id})
			arr.push({pn: "format", pv: 'JSON'})
			arr.push({pn: "session_key", pv: vars['session_key'] })
			arr.sortOn('pn');
					  
			var url_vars:URLVariables = new URLVariables();
			for(var i:int = 0; i < arr.length; i++ ) {
				url_vars[ (arr[i].pn) ] = arr[ i ].pv;
			}
					  
			url_vars["sig"] = makeSig( arr );
			return url_vars;
		}
		
		override public function makeSig( arr:Array ):String {
			return super.makeSig(arr).toLowerCase();
		}
		
		override protected function getSig( arr: Array): String {
			var query: String = '';
			for( var i:uint = 0; i < arr.length; i++ ) {
				query += arr[i].pn + '=' + arr[i].pv;
			}
			return query + secret;
		}
		
		override public function createRequest(a_vars: Array, a_url: String='', method: String = URLRequestMethod.GET): URLRequest {
			var result: URLRequest = super.createRequest(a_vars, a_url, method);
			_call_id++;
			return result;
		}
		
	}
}